<?php
header('Content-type:text/html;charset=utf8');
/**
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-4-25
*/
/**
 * 
 * 导航控制器
 * @author xjh
 *
 */
class NavController extends Controller{
	/**
	 * 访问控制过滤
	 * @author xjh
	 */
	function filters(){
		return array(
			'accessControl'
		);
	}
	/**
	 * 具体方法访问具体条件
	 */
	
	function accessRules(){
		return array(
			array(
				'allow',
				'actions'=>array(''),
				'users'=>array('@'),//登陆系统的用户
			),
//			array(
//				'deny',
//				'users'=>array('*'),//所有用户
//			),
		);
	}

	/**
	 * 顶部信息
	 * 
	 */
	function actionsearchNavTop(){
		$this->renderPartial('searchNavTop');
	}

	/**
	 * 导航删除
	 */
	function actionNavDel($nav_id){
		$nav_model = Nav::model();
		$row = $nav_model->NavDel($nav_id);
		if($row){
			echo 1;
		}else{
			echo 0;
		}
//		 $this->redirect('./index.php?r=backweb/Nav/nav');
	}

	/**
	 * 导航修改
	 */
	function actionNavUpd($nav_id){
		$nav_model = new Nav();
		$info = $nav_model->parentAll();
		$nav_model = Nav::model();
		$nav_info = $nav_model->getNavById($nav_id);
		if(isset($_POST) && @$_POST['send'] == "修改"){
			$row = $nav_model->NavUpdate($_POST,$nav_id);
			if($row){
				$this->redirect("./index.php?r=backweb/nav/nav");
			}else{
				echo "<script>alert('修改失败');</script>";
			}
		}
		$this->renderPartial('updatenav',array('parent_all' => $info,'nav_info'=>$nav_info[0]));
	}
	
	/**
	 * 获得二级搜索导航
	 */
	function actiongetChildNav($nav_id){
		
		
			$nav_model = new Nav();	
			$child_nav = $nav_model -> getChildNav($nav_id);
			$parent_name = $nav_model->getNavById($nav_id);
			$parent_name = $parent_name[0]['nav_name'];
			echo "<h4>".$parent_name."<em><a href='#'>更多</a></em></h4>";
			echo "<div class='line'></div>";
			echo "<p>";
			for ($i = 0; $i < count($child_nav)-1; $i++) {
				echo "<a href='./index.php?r=goods/category&kkeyword=".$child_nav[$i]['nav_name']."'>".$child_nav[$i]['nav_name']."</a>|";
			}
				echo "<a href='./index.php?r=goods/category&kkeyword=".$child_nav[count($child_nav)-1]['nav_name']."'>".$child_nav[count($child_nav)-1]['nav_name']."</a>";
	}
	

	
	/**
	 * 是否有效的切换
	 */
	function actionActive($nav_id,$active_id){
		$active = new active();
		$active -> setActive($nav_id,$active_id,"nav","nav_id");
		$this->redirect("./index.php?r=backweb/nav/nav");
	}
	
	/**
	 * 商品品牌控制器
	 * @author xjh
	 */
	function actionnav(){
		$nav_model = Nav::model();
		if(@$_POST['send'] == '更新信息'){
			for ($i=0; $i < count($_POST['nav_id']); $i++) { 
				$row = $nav_model -> updateNav($_POST['nav_id'][$i],$_POST['nav_position'][$i],$_POST['sort'][$i]);
			}
		}
		if(@$_POST['btquery'] == '查询'){
			$infos = $nav_model->pageSearch($_POST);
		}else{
			$infos = $nav_model->all();
		}
		$parent_name = $nav_model->getParentName();
		$parent = array();
		
		
		
		//重新拼装父级id和name关系
		foreach($parent_name as $_k =>$_v){
			$parent[$_v['nav_id']] = $_v['nav_name'];
		}

		$page_list = $nav_model->page_list;
		$infos_count = $nav_model->infos_count;
		$active = array(
			0=>'否',
			1=>'是',
		);
		$obj = new CDbConnection();
		$topInfo = $obj -> getStats();
		$number = $topInfo[0];
		$times = $topInfo[1];
		$this->renderPartial('nav',array('infos'=>$infos,'active'=>$active,'info_count'=>$infos_count[0]['count(*)'],'page_list'=>$page_list,'number'=>$number,'times'=>$times,'parent'=>$parent));
	}
	
	function actiontoAddNav(){
		$nav_model = new Nav();
		$info = $nav_model->parentAll();
		$this->renderPartial('navadd',array('parent_all'=>$info));
	}
	/*
	 * 导航添加控制器
	 * @author xjh
	 */
	function actionAddNav(){
		$nav_model = new Nav();
		$infos = $nav_model->checkExist($_POST);
			if($_POST['btsave'] == '添加'){
				if(!empty($infos)){
					echo "<script>alert('导航已存在');</script>";
					echo "操作错误，请返回";
					exit;
				}
				if($nav_model->addNav($_POST)){
					echo "<script>alert('导航添加成功');</script>";
					 $this->renderPartial('searchNavTop');
				}else{
					echo "<script>alert('添加失败');</script>";
				}
			}else{
				echo "非法操作,请返回继续操作";
				exit;
			}
		
	}
}
?>